package crmdna.interaction;

import static crmdna.common.AssertUtils.ensure;
import static crmdna.common.AssertUtils.ensureNotNull;

import java.util.Date;

import crmdna.client.Client;
import crmdna.member.MemberLoader;
import crmdna.user.User;
import crmdna.user.User.Action;
import crmdna.user.User.ResourceType;

public class Interaction {

	public enum InteractionType {
		PHONE, EMAIL, FB, GOOGLEPLUS, SMS, FACETOFACE, WEBSITE, OTHER
	}

	public static InteractionProp createInteraction(String client,
			long memberId, String content, InteractionType interactionType,
			Date timestamp, String login) {

		Client.ensureValid(client);
		User.ensureValidUser(client, login);
		ensureNotNull(interactionType, "interactionType is null");

		MemberLoader.safeGet(client, memberId, login);
		long userId = User.safeGet(client, login).toProp().userId;

		if (timestamp == null)
			timestamp = new Date(); // current time

		InteractionProp interactionProp = InteractionCore.createInteraction(
				client, memberId, content, interactionType.toString(),
				timestamp, userId, login);

		return interactionProp;
	}

	public static void deleteInteraction(String client, long interactionId,
			String login) {
		Client.ensureValid(client);
		User.ensureValidUser(client, login);

		long userId = User.safeGet(client, login).toProp().userId;

		ensure(interactionId != 0, "interaction id is 0");

		InteractionEntity entity = InteractionCore.safeGet(client,
				interactionId);

		// need previlage for deleting someone else's interaction
		if (entity.userId != userId) {
			User.ensureAccess(client, login, ResourceType.INTERACTION, 0,
					Action.WRITE);
		}

		InteractionCore.deleteInteraction(client, interactionId);
	}

	public static InteractionProp updateInteraction(String client,
			long interactionId, Long newMemberId,
			InteractionType newInteractionType, String newUser, String login) {

		Client.ensureValid(client);
		User.ensureValidUser(client, login);

		InteractionEntity entity = InteractionCore.safeGet(client,
				interactionId);
		
		if (newMemberId != null)
			MemberLoader.safeGet(client, newMemberId, login);

		long userId = User.safeGet(client, login).toProp().userId;

		// need privilege to update someone else's interaction
		if (entity.userId != userId)
			User.ensureAccess(client, login, ResourceType.INTERACTION, 0,
					Action.WRITE);

		Long newUserId = null;
		if (newUser != null) {
			newUserId = User.safeGet(client, newUser).toProp().userId;
			ensureNotNull(newUserId);

			if (newUserId != entity.userId)
				User.ensureAccess(client, login, ResourceType.INTERACTION, 0,
						Action.WRITE);
		}

		return InteractionCore.updateInteraction(client, interactionId,
				newMemberId,
				newInteractionType != null ? newInteractionType.toString()
						: null, newUserId, newUser);
	}

	public static InteractionQueryResult query(String client,
			InteractionQueryCondtion qc, String login) {
		Client.ensureValid(client);
		User.ensureValidUser(client, login);

		InteractionQueryResult result = InteractionCore.query(client, qc);
		return result;
	}

	public static void createSubInteraction(String client, long interactionId,
			String content, Date timestamp, String login) {

		Client.ensureValid(client);
		User.ensureValidUser(client, login);
		ensure(interactionId != 0, "interactionId is 0");

		InteractionEntity interactionEntity = InteractionCore.safeGet(client,
				interactionId);

		long userId = User.safeGet(client, login).toProp().userId;

		if (interactionEntity.userId != userId)
			User.ensureAccess(client, login, ResourceType.INTERACTION, 0,
					Action.WRITE);

		if (null == timestamp)
			timestamp = new Date();

		InteractionCore.createSubInteraction(client, interactionId, content,
				timestamp);
	}

	public static void deleteSubInteraction(String client, long interactionId,
			long subinteractionId, String login) {

		Client.ensureValid(client);
		User.ensureValidUser(client, login);
		ensure(interactionId != 0, "interactionId is 0");

		InteractionEntity entity = InteractionCore.safeGet(client,
				interactionId);

		long userId = User.safeGet(client, login).toProp().userId;

		if (entity.userId != userId)
			User.ensureAccess(client, login, ResourceType.INTERACTION, 0,
					Action.WRITE);

		InteractionCore.deleteSubInteraction(client, interactionId,
				subinteractionId);
	}

	public static void updateSubInteraction(String client, long interactionId,
			long subInteractionId, String content, String login) {

		Client.ensureValid(client);

		InteractionEntity interactionEntity = InteractionCore.safeGet(client,
				interactionId);

		long userId = User.safeGet(client, login).toProp().userId;

		if (interactionEntity.userId != userId)
			User.ensureAccess(client, login, ResourceType.INTERACTION, 0,
					Action.WRITE);

		InteractionCore.updateSubInteraction(client, interactionId,
				subInteractionId, content, null);
	}
}
